﻿Imports System.Data.OleDb

Public Class DAOHocSinh
    Public Sub New()

    End Sub
    Public Function Connection(ByVal path As String) As OleDbConnection
        Return New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path)
    End Function
    Public Function LoadMa(ByVal Ma As Long, ByVal path As String) As DataTable
        Dim dset As DataSet = New DataSet
        Dim strQuery As String = "Select * from HocSinh where MaHS=" + Ma.ToString()
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "HocSinh")
        Return dset.Tables("HocSinh")
    End Function
    Public Function LoadTable(ByVal strQuery As String, ByVal path As String) As DataTable
        Dim dset As DataSet = New DataSet
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim ad As OleDbDataAdapter = New OleDbDataAdapter(cmd)
        conn.Close()
        ad.Fill(dset, "HocSinh")
        Return dset.Tables("HocSinh")
    End Function
    Public Function Insert(ByVal Hs As DTOHocSinh, ByVal path As String) As Boolean
        Dim strQuery As String = "Insert into HocSinh(TenHS,NgaySinh,DiaChi,GioiTinh,Email,TenLop) values('" + Hs.TenHS + "','" + Hs.NgaySinh.ToString() + "','" + Hs.DiaChi + "','" + Hs.GioiTinh + "','" + Hs.Email + "','" + Hs.TenLop + "')"
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
    Public Function Delete(ByVal MaHS As Long, ByVal path As String) As Boolean
        Dim strQuery As String = "Delete from HocSinh where MaHS=" + MaHS.ToString()
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
    Public Function Update(ByVal hs As DTOHocSinh, ByVal path As String) As Boolean
        Dim strQuery As String = "Update HocSinh set TenHS='" + hs.TenHS.ToString() + "',DiaChi='" + hs.DiaChi.ToString() + "',GioiTinh='" + hs.GioiTinh.ToString() + "',Email='" + hs.Email.ToString() + "',NgaySinh='" + hs.NgaySinh.ToString() + "',TenLop='" + hs.TenLop + "' where MaHS=" + hs.MaHS.ToString()
        Dim conn As OleDbConnection = Me.Connection(path)
        conn.Open()
        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, conn)
        Dim flag As Boolean = False
        Try
            flag = cmd.ExecuteNonQuery() > 0
        Catch ex As Exception
            flag = False
        End Try

        conn.Close()
        Return flag
    End Function
End Class
